package systemServiceImpl

import (
	"baize/app/business/system/systemMapper"
	"baize/app/business/system/systemMapper/systemMapperImpl"
	"baize/app/business/system/systemModels"
	"baize/app/utils/cache"
	"baize/app/utils/excel"
	"baize/app/utils/response"
	"baize/app/utils/snowflake"
	"bytes"
	"compress/gzip"
	"context"
	"encoding/json"
	"github.com/gin-gonic/gin"
)

type DictService struct {
	dictMapper systemMapper.IDictMapper
	dictKey    string
	gzipNil    []byte
}

func NewDictService(dtm *systemMapperImpl.SysDictMapper) *DictService {
	return &DictService{
		dictMapper: dtm,
		dictKey:    "sys_dict:",
		gzipNil:    []byte{31, 139, 8, 0, 0, 0, 0, 0, 2, 255, 170, 86, 74, 206, 79, 73, 85, 178, 50, 50, 48, 208, 81, 202, 45, 78, 87, 178, 82, 42, 46, 77, 78, 78, 45, 46, 86, 170, 5, 4, 0, 0, 255, 255, 166, 20, 213, 245, 28, 0, 0, 0},
	}
}

func (dictService *DictService) SelectDictTypeList(c *gin.Context, dictType *systemModels.SysDictTypeDQL) (list []*systemModels.SysDictType, count int64) {
	return dictService.dictMapper.SelectDictTypeList(c, dictType)
}
func (dictService *DictService) ExportDictType(c *gin.Context, dictType *systemModels.SysDictTypeDQL) (data []byte) {
	list := dictService.dictMapper.SelectDictTypeAll(c, dictType)
	toExcel, err := excel.SliceToExcel(list)
	if err != nil {
		panic(err)
	}
	buffer, err := toExcel.WriteToBuffer()
	if err != nil {
		panic(err)
	}
	return buffer.Bytes()
}

func (dictService *DictService) SelectDictTypeById(c *gin.Context, dictId int64) (dictType *systemModels.SysDictType) {
	return dictService.dictMapper.SelectDictTypeById(c, dictId)

}
func (dictService *DictService) SelectDictTypesByIds(c *gin.Context, dictId []int64) (dictTypes []string) {
	return dictService.dictMapper.SelectDictTypesByIds(c, dictId)

}
func (dictService *DictService) InsertDictType(c *gin.Context, dictType *systemModels.SysDictType) {
	dictType.DictId = snowflake.GenID()
	dictService.dictMapper.InsertDictType(c, dictType)
}

func (dictService *DictService) UpdateDictType(c *gin.Context, dictType *systemModels.SysDictType) {
	dictService.dictMapper.UpdateDictType(c, dictType)
}
func (dictService *DictService) DeleteDictTypeByIds(c *gin.Context, dictIds []int64) {
	dictService.dictMapper.DeleteDictTypeByIds(c, dictIds)
}

func (dictService *DictService) CheckDictTypeUnique(c *gin.Context, id int64, dictType string) bool {
	dictId := dictService.dictMapper.CheckDictTypeUnique(c, dictType)
	if dictId == id || dictId == 0 {
		return false
	}
	return true
}
func (dictService *DictService) DictTypeClearCache(c *gin.Context) {
	cache.GetCache().Del(c, dictService.dictKey+"*")
}

func (dictService *DictService) SelectDictTypeAll(c *gin.Context) (list []*systemModels.SysDictType) {
	return dictService.dictMapper.SelectDictTypeAll(c, new(systemModels.SysDictTypeDQL))
}

func (dictService *DictService) SelectDictDataByType(c *gin.Context, dictType string) (data []byte) {

	data = dictService.getDictCache(c, dictType)
	if data != nil && len(data) != 0 {
		return
	}
	sysDictDataList := dictService.dictMapper.SelectDictDataByType(c, dictType)
	if len(sysDictDataList) != 0 {
		responseData := response.ResponseData{Code: response.Success, Msg: response.Success.Msg(), Data: sysDictDataList}
		marshal, err := json.Marshal(responseData)
		if err != nil {
			panic(err)
		}
		var buf bytes.Buffer
		gz, err := gzip.NewWriterLevel(&buf, gzip.BestCompression)
		if err != nil {
			panic(err)
		}

		if _, err = gz.Write(marshal); err != nil {
			panic(err)
		}
		if err = gz.Close(); err != nil {
			panic(err)
		}
		compressedData := buf.Bytes()
		go cache.GetCache().Set(context.Background(), dictService.dictKey+dictType, string(compressedData), 0)
		return compressedData
	}
	return dictService.gzipNil
}
func (dictService *DictService) SelectDictDataList(c *gin.Context, dictData *systemModels.SysDictDataDQL) (list []*systemModels.SysDictData, count int64) {
	return dictService.dictMapper.SelectDictDataList(c, dictData)

}
func (dictService *DictService) ExportDictData(c *gin.Context, dictData *systemModels.SysDictDataDQL) (data []byte) {
	list, _ := dictService.dictMapper.SelectDictDataList(c, dictData)
	toExcel, err := excel.SliceToExcel(list)
	if err != nil {
		panic(err)
	}
	buffer, err := toExcel.WriteToBuffer()
	if err != nil {
		panic(err)
	}
	return buffer.Bytes()
}
func (dictService *DictService) SelectDictDataById(c *gin.Context, dictCode int64) (dictData *systemModels.SysDictData) {
	return dictService.dictMapper.SelectDictDataById(c, dictCode)
}

func (dictService *DictService) InsertDictData(c *gin.Context, dictData *systemModels.SysDictData) {
	dictData.DictCode = snowflake.GenID()
	dictService.dictMapper.InsertDictData(c, dictData)
	cache.GetCache().Del(c, dictService.dictKey+"*")
}

func (dictService *DictService) UpdateDictData(c *gin.Context, dictData *systemModels.SysDictData) {
	dictService.dictMapper.UpdateDictData(c, dictData)
	cache.GetCache().Del(c, dictService.dictKey+"*")
}
func (dictService *DictService) DeleteDictDataByIds(c *gin.Context, dictCodes []int64) {
	dictService.dictMapper.DeleteDictDataByIds(c, dictCodes)
	cache.GetCache().Del(c, dictService.dictKey+"*")
}
func (dictService *DictService) CheckDictDataByTypes(c *gin.Context, dictType []string) bool {
	return dictService.dictMapper.CheckDictDataByTypes(c, dictType)
}
func (dictService *DictService) getDictCache(c context.Context, dictType string) (dictDataList []byte) {
	getString, err := cache.GetCache().Get(c, dictService.dictKey+dictType)
	if err != nil {
		return nil
	}
	return []byte(getString)
}
